package com.eastcom.collection.script

import java.awt.TexturePaintContext.Int;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.InetAddress.Cache;
import java.sql.Date;
import java.sql.PreparedStatement
import java.sql.SQLException
import java.sql.Timestamp
import java.sql.Types;
import java.text.SimpleDateFormat
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlValue;

import com.eastcom.collection.tools.db.DbEngine
import com.eastcom.groovy.provider.*
import com.eastcom.groovy.tools.mail.*
import com.sun.java.util.jar.pack.Instruction.Switch;
import com.sun.org.apache.bcel.internal.generic.NEW;
import com.sun.org.apache.bcel.internal.util.ClassPath.Dir;
import com.eastcom.collection.provider.*
import com.eastcom.collection.config.FtpTaskConfig
import com.eastcom.collection.config.PmFile
import com.eastcom.collection.context.*
import com.eastcom.collection.tools.file.*

import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory;
import org.apache.commons.net.ftp.FTPFile
import org.apache.commons.net.ftp.FTPFileFilter
import org.springframework.dao.DataAccessException
import org.springframework.jdbc.core.support.AbstractLobCreatingPreparedStatementCallback
import org.springframework.jdbc.support.lob.LobCreator
import org.springframework.jdbc.support.lob.OracleLobHandler
import org.springframework.jdbc.support.nativejdbc.CommonsDbcpNativeJdbcExtractor
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
/**
 * 任务类，可配置
 * @author yuanyue
 *
 */
//CMCC-ADSL|QQ大乐斗|0|0|1|1|2|2|3|3|4|4|5|5|6|6|7|7|8|8|9|9|10|10|11|11|12|12|13|13|14|14|15|15|16|16|17|17|18|18|19|19|20|20|21|21|22|22|23|23
//CMCC-ADSL|QQ飞车|0|0|1|1|2|2|3|3|4|4|5|5|6|6|7|7|8|8|9|9|10|10|11|11|12|12|13|13|14|14|15|15|16|16|17|17|18|18|19|19|20|20|21|21|22|22|23|23
//CMCC-ADSL|反恐精英|0|0|1|1|2|2|3|3|4|4|5|5|6|6|7|7|8|8|9|9|10|10|11|11|12|12|13|13|14|14|15|15|16|16|17|17|18|18|19|19|20|20|21|21|22|22|23|23
//CMCC-ADSL|地下城与勇士|0|0|1|1|2|2|3|3|4|4|5|5|6|6|7|7|8|8|9|9|10|10|11|11|12|12|13|13|14|14|15|15|16|16|17|17|18|18|19|19|20|20|21|21|22|22|23|23
//CMCC-ADSL|大话水浒|0|0|1|1|2|2|3|3|4|4|5|5|6|6|7|7|8|8|9|9|10|10|11|11|12|12|13|13|14|14|15|15|16|16|17|17|18|18|19|19|20|20|21|21|22|22|23|23
//CMCC-ADSL|幻想三国|0|0|1|1|2|2|3|3|4|4|5|5|6|6|7|7|8|8|9|9|10|10|11|11|12|12|13|13|14|14|15|15|16|16|17|17|18|18|19|19|20|20|21|21|22|22|23|23
//CMCC-ADSL|战地之王|0|0|1|1|2|2|3|3|4|4|5|5|6|6|7|7|8|8|9|9|10|10|11|11|12|12|13|13|14|14|15|15|16|16|17|17|18|18|19|19|20|20|21|21|22|22|23|23
//CMCC-ADSL|手机QQ游戏|0|0|1|1|2|2|3|3|4|4|5|5|6|6|7|7|8|8|9|9|10|10|11|11|12|12|13|13|14|14|15|15|16|16|17|17|18|18|19|19|20|20|21|21|22|22|23|23
//CMCC-ADSL|英雄联盟|0|0|1|1|2|2|3|3|4|4|5|5|6|6|7|7|8|8|9|9|10|10|11|11|12|12|13|13|14|14|15|15|16|16|17|17|18|18|19|19|20|20|21|21|22|22|23|23
//CMCC-ADSL|穿越火线|0|0|1|1|2|2|3|3|4|4|5|5|6|6|7|7|8|8|9|9|10|10|11|11|12|12|13|13|14|14|15|15|16|16|17|17|18|18|19|19|20|20|21|21|22|22|23|23
class CopyOfSimpleXmlFileParseTaskDirector {
	/**
	 * 使用jaxb解析xml再入库
	 */
	/************************配置属性，可以修改，请尽量使用英文防止中文编码问题***************************/
	List<String> xmlFilePath
	String localRootPath
	final static String dstdbBean = "oracle_39_nhm"
	String encode
	String timeFormate
	static String insertSql1 = "insert into NHM_PM_UNIT_COLLECT_KPI_GROUP(BEGINTIME,ENDTIME,INFOMODELREFERENCED,DNPREFIX,SENDERNAME,VENDORNAME,JOBID,OBJECTTYPE,GROUP_ID) values(?,?,?,?,?,?,?,?,?)"
	static String insertSql2 = "insert into NHM_PM_UNIT_COLLECT_KPI_DETAIL(GROUP_ID,PM_NAME,PM_DN,PM_USERLABEL,PM_VALUE) values(?,?,?,?,?)"
	static String updateSql = "update nhm_pm_unit_collect_task d set d.status = 'ok' where d.taskid = ?"
	/***************************可配置部分结束***********************************************/

















	ThreadPoolTaskExecutor threadPoolTaskExecutor
	static List<String> dstFieldList1 = new ArrayList()
	static List<Integer> dstFieldTypeList1 = new ArrayList()
	static List<String> dstFieldList2 = new ArrayList()
	static List<Integer> dstFieldTypeList2 = new ArrayList()
	static int[] sqlTypes1
	static int[] sqlTypes2
	List<List<String>> parseResultList = new ArrayList()
	Log logger = LogFactory.getLog(CopyOfSimpleXmlFileParseTaskDirector.class)

	/*********************initJob和doJob是要被程序调用的方法**************************/
	public boolean initJob(String jobId){
		insertSql1 = DbEngine.doDbInit(dstdbBean, dstFieldList1, dstFieldTypeList1, insertSql1, true)
		sqlTypes1 = new int[dstFieldTypeList1.size()]
		for(int i = 0; i < dstFieldTypeList1.size(); i++){
			sqlTypes1[i] = dstFieldTypeList1.get(i)
		}

		insertSql2 = DbEngine.doDbInit(dstdbBean, dstFieldList2, dstFieldTypeList2, insertSql2, true)
		sqlTypes2 = new int[dstFieldTypeList2.size()]
		for(int i = 0; i < dstFieldTypeList2.size(); i++){
			sqlTypes2[i] = dstFieldTypeList2.get(i)
		}
	}

	public boolean doJob(String jobId){
		ConcurrentLinkedQueue parseTaskWorkQueue = SpringContextProvider.getBean("parseTaskWorkQueue")
		if(parseTaskWorkQueue.isEmpty()){
			return true
		}
		FtpTaskConfig ftpTaskCf = parseTaskWorkQueue.poll()
		List<String> filePath = new ArrayList()
		ftpTaskCf.fileNameList.each{
			filePath.add(it)
		}
		this.localRootPath = ftpTaskCf.localPath
		this.xmlFilePath = filePath
		this.encode = ftpTaskCf.encode
		SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
		for(int i = 0; i < xmlFilePath.size(); i++){
			try {
				String filePh = localRootPath+"/"+xmlFilePath[i]
				jobId = xmlFilePath[i]
				if(xmlFilePath[i].endsWith(".gz")){
					FileUtils.unCompressGzFile(localRootPath+"/"+xmlFilePath[i])
					filePh = localRootPath+"/"+xmlFilePath[i].substring(0, xmlFilePath[i].length()-3)
				}
				PmFile pmFile = JaxbUtil.converyFileToJavaBean(filePh, encode, PmFile.class)

				DbEngine dbEngine = SpringContextProvider.getBean("dbEngine")
				dbEngine.getJdbcUtil().setDataSource(DynamicBeanDomianProvider.getBean(dstdbBean))
				Object[] param = new Object[9]
				param[0] = new Timestamp(sim.parse(pmFile.FileHeader.BeginTime).getTime())
				for(int j = 0; j <pmFile.Measurements.size(); j++){
					param[1] = new Timestamp(sim.parse(pmFile.FileHeader.EndTime).getTime())
					param[2] = pmFile.FileHeader.InfoModelReferenced
					param[3] = pmFile.FileHeader.NeType
					param[4] = pmFile.FileHeader.OmcName
					param[5] = pmFile.FileHeader.VendorName
					param[6] = pmFile.FileHeader.JobId
					param[7] = pmFile.Measurements[j].ObjectType
					int[] type = [Types.TIMESTAMP,Types.TIMESTAMP,Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,Types.VARCHAR]
					for(int k = 0; k < pmFile.Measurements[j].PmData.Pm.size(); k++){
						param[8] = jobId+j+pmFile.Measurements[j].PmData.Pm[k].UserLabel
						dbEngine.getJdbcUtil().update(insertSql1, param, type)
						for(int l = 0; l < pmFile.Measurements[j].PmName.N.size(); l++){
							String GROUP_ID = jobId+j+pmFile.Measurements[j].PmData.Pm[k].UserLabel
							String PM_NAME = pmFile.Measurements[j].PmName.N[l].value
							int id = Integer.valueOf(pmFile.Measurements[j].PmName.N[l].i)
							String PM_DN = pmFile.Measurements[j].PmData.Pm[k].Dn
							String PM_USERLABEL = pmFile.Measurements[j].PmData.Pm[k].UserLabel
							String PM_VALUE
							boolean isFindById = false
							if(pmFile.Measurements[j].PmData.Pm[k].V != null){
								for(int m = 0; m < pmFile.Measurements[j].PmData.Pm[k].V.size(); m++){
									if(Integer.valueOf(pmFile.Measurements[j].PmData.Pm[k].V[m].i) == id){
										PM_VALUE = pmFile.Measurements[j].PmData.Pm[k].V[m].value
										isFindById = true
										break
									}
								}
							}
							if(! isFindById){
								if(pmFile.Measurements[j].PmData.Pm[k].CV != null){
									for(int m = 0; m <pmFile.Measurements[j].PmData.Pm[k].CV.size(); m++){
										if(Integer.valueOf(pmFile.Measurements[j].PmData.Pm[k].CV[m].i) == id){
											for(int n = 0; n < pmFile.Measurements[j].PmData.Pm[k].CV[m].SN.size(); n++){
												PM_VALUE = pmFile.Measurements[j].PmData.Pm[k].CV[m].SV[n]
												//注意，遗留的问题，需要想办法把所有的伪测量值入库
												break
											}
											break
										}
									}
								}
							}
							Object[] KpiParam = new Object[5]
							KpiParam[0] = GROUP_ID
							KpiParam[1] = PM_NAME
							KpiParam[2] = PM_DN
							KpiParam[3] = PM_USERLABEL
							KpiParam[4] = PM_VALUE
							int[] kpiType = [Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,Types.VARCHAR]
							dbEngine.getJdbcUtil().update(insertSql2, KpiParam, kpiType)
							
							Object[] taskParam = new Object[1]
							int[] taskType = new int[1]
							taskParam[0] = ftpTaskCf.taskId
							taskType[0] = Types.VARCHAR
							dbEngine.getJdbcUtil().update(updateSql,taskParam,taskType)
						}
					}
				}
			} catch (Exception e) {
				logger.error(this, e);
			}

		}
	}


}


